package artibella.media.controls.ui 
{
	import artibella.text.FontsManager;
	import artibella.text.FormattedTextField;
	import artibella.text.StylesManager;
	import artibella.utils.StringsUtils;
	
	import flash.display.MovieClip;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class TimeTool extends MovieClip
	{
		public static const TIME		:String = "time";
		public static const DURATION	:String = "duration";
		public static const FULL		:String = "full";
		
		private var _displayMode:String;
		private var _duration:String;
		
		private var _chp:FormattedTextField;
		private var _defaultTxt:String = "...";
		
		private var _format:TextFormat;
		
		public function TimeTool( mode:String = TimeTool.FULL, format:TextFormat = null ) 
		{
			_format = format;
			_displayMode = mode;
			
			_init();
		}
		
		private function _init():void
		{
			if ( _format == null ) _format = StylesManager.instance.getStyleFormat( ".player_time_tool" );

			_chp = new FormattedTextField( _defaultTxt, _format );
			_chp.x = 0;
			_chp.y = - _chp.height / 2;
			
			addChild( _chp );
		}
		
		// - - -
		
		public function set duration( d:int ):void
		{
			//_duration = StringsUtils.hmsFormat( d / 1000 );
			_duration = StringsUtils.hmsFormat( d );
			
			timeUpdate();
		}
		
		public function timeUpdate( p:int = 0 ):void
		{
			//trace( p );
			
			var format:TextFormat = _chp.getTextFormat();
			
			var txt:String;
			
			switch( _displayMode )
			{
				case TIME :
					txt = StringsUtils.hmsFormat( p / 1000 );
					break;
				
				case DURATION :
					txt = ( _duration != null ) ? _duration : _defaultTxt;
					break;
					
				case FULL :
					txt = StringsUtils.hmsFormat( p / 1000 ) + " / " + ( _duration != null ? _duration : _defaultTxt );
					break;
			}
			
			_chp.text = txt;
			
			_chp.setTextFormat( format );
		}
		
		// - - -
		
		public function reset():void
		{
			var format:TextFormat = _chp.getTextFormat();
			_chp.text = _defaultTxt;
			_chp.setTextFormat( format );
		}
		
	}

}